

/*签约运营账号管理*/
//初始化客户归属下拉列表
function ascription() {
    var user_data = {
    }
    var succCallBack = function (data, status, response) {
        //console.log(data)
        if(data.errorCode=="0") {
            var dataInter=data.group;
            var interText = doT.template($("#ascription_bob").text());
            $("#ascription_lsit").html(interText(dataInter));
        }
    };
    return web.doAjax(root.inter.getGroup, 'post', user_data, succCallBack);
}


var dataSource = new Array();
//table的数据源
var table;   //table对象
dataSource = [];
function operatesearch() {
    $.ajax({
        url: 'https://'+ window.location.host +'/api/admin/amembership/crmAccount/listPage',
        cache: false,  //禁用缓存
        type: "POST",
        dataType:'json',
        contentType: "application/json",
        data: JSON.stringify({
            "pageNum":1,
            "pageSize":30,
            "loginAccount":$("#loginaccount").val(),
            "customerName":$("#customerName").val(),
            "customerType":$("#customerType").val(),
            "status":$("#statuss").val(),
            "belongGroupId":$("#ascription_lsit").val()

        }),
        success: function (data) {
            console.log(data)
            couponlist1(data.data, 0);
            $('.M-box3').pagination({
                pageCount: data.data.pageCount,
                jump: true,
                coping: true,
                prevContent: '上页',
                nextContent: '下页',
                callback: function (api) {
                    console.log(api)
                    $.ajax({
                        url: 'https://'+ window.location.host +'/api/admin/amembership/crmAccount/listPage',
                        cache: false,  //禁用缓存
                        type: "POST",
                        dataType:'json',
                        contentType: "application/json",
                        data: JSON.stringify({
                            "pageNum":api.getCurrent(),
                            "pageSize":30,
                            "loginAccount":$("#loginaccount").val(),
                            "customerName":$("#customerName").val(),
                            "customerType":$("#customerType").val(),
                            "status":$("#statuss").val(),
                            "belongGroupId":$("#ascription_lsit").val()

                        }),
                        success: function (result) {
                            console.log(result)
                            couponlist1(result.data);
                        },
                        error: function (XMLHttpRequest, textStatus, errorThrown) {
                            alert(XMLHttpRequest);
                        }
                    });
                }
            });

        },
        error: function () {
            $.Huimodalalert('失败！',2000);
        }
    });
}

// var j = 0;
function couponlist1(data, index){
    sessionStorage.setItem("authorizedUsers", JSON.stringify(data));
    dataSource.splice(0,dataSource.length);//清空数组
    var datashu =data.startRow+1;
    $.each(data.data, function (i, field) {
        var datashus = datashu++
        var rowData = new Array();
        rowData.push(field.loginAccount);
        rowData.push(field.customerName);
        rowData.push(field.customerGrade);
        rowData.push(field.organizationName);
        rowData.push(field.customerType);
        rowData.push(field.status);
        rowData.push(field.id);
        rowData.push(datashus);
        dataSource.push(rowData);

    });

    initData(dataSource);

    function initData(dataSource) {

        var dttable = $('#tabletables').dataTable(); // 初始化dataTable，清空上一个值
        dttable.fnClearTable(); //清空一下table
        dttable.fnDestroy(); //还原初始化了的datatable
        $("#tabletables").DataTable({
            "autoWidth": false,
            "ordering": false,
            "searching": false,
            "paging": false,
            "retrieve": true,
            "destroy": true,
            "bAutoWidth": true,
            "bLengthChange": true,
            "aLengthMenu": false,
            "oLanguage": {
                "sLengthMenu": "每页显示 _MENU_ 条记录",
                "sZeroRecords": "抱歉， 没有找到",
                "sInfo": "从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                "sInfoEmpty": "没有数据",
                "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                "oPaginate": {
                    "sFirst": "首页",
                    "sPrevious": "前一页",
                    "sNext": "后一页",
                    "sLast": "尾页"
                },
                "sZeroRecords": "没有检索到数据",
            },
            data: dataSource,
            columnDefs: [
                {
                    "visible": true,
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[7]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 0
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[0]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 1
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[1]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 2
                },
            /*    {
                    "render": function (data, type, row, meta) {
                        var val = row[2]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },*/
                {
                    "render": function (data, type, row, meta) {
                        var val = row[3]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets": 3
                },
                {
                    "render": function (data, type, row, meta) {
                        var val = row[4]
                        if (val == undefined) {
                            val = ''
                        }
                        return val;
                    },
                    "targets":4
                },
                {
                    "render": function (data, type, row, meta) {
                        if(row[5]==10){
                            var val = '<a  style="margin-right: 20px" class="powerButton 523"><span class="list" id="'+row[6]+'" onclick="Closes('+row[6]+')">启用</span></a>'
                            return val;
                        }else {
                            var val ='<a  style="margin-right: 20px" class="powerButton 523"><span class="list" id="'+row[6]+'" onclick="opens('+row[6]+')">禁用</span></a>'
                            return val;
                        }
                    },
                    "targets": 5
                },


                {
                    "render": function (data, type, row, meta) {
                        var val = '<a  style="margin-right: 20px" class="powerButton 524"><span class="list" onclick="resetpassword(\''+row[6]+'\',\''+row[0]+'\')">重置密码</span></a>'
                        return val;
                    },
                    "targets":6
                },



            ]
        });
        //权限控制函数
        power()
    };
}
//关闭弹框
var index=0;
function Closes(id,val) {
    var curInd = layer.confirm('<span id="status" style="width:200px;height:100px;resize: none;" maxlength="200" >确定要禁用账号吗？</span>', {title:'提示',btn: ['确定', '取消']}, function(index){

        Closes1(id,curInd,val)
        layer.close(curInd);
    },function(){
        layer.close(curInd);

    });
}
//第一个
function Closes1(id,val){
    var target = document.getElementById(''+id+'');
    console.log(target.parentElement.innerHTML)
    target.parentElement.innerHTML='禁用';
    console.log(target)

    var reason=$('#reason'+id+'').val();
    console.log(reason)
        $.ajax({
            contentType: "application/json;charset=UTF-8",
            url:'https://'+ window.location.host +'/api/admin/amembership/crmAccount/updateStatus',
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            data: JSON.stringify({
                "id":id,
                "status":20,

            }),
            success: function (data) {
                console.log(data)

                target.onclick = null;
                target.setAttribute("onclick", 'confirm('+id+')')
                operatesearch();

            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        })
    layer.close(index);
}

//开启弹框
var index=0;
function opens(id,val) {
    var curInd = layer.confirm('<span id="status" style="width:200px;height:100px;resize: none;" maxlength="200" >确定要启用账号吗？</span>', {title:'提示',btn: ['确定', '取消']}, function(index){

        opens2(id,curInd,val)
        layer.close(curInd);
    },function(){
        layer.close(curInd);

    });
}
//第一个
function opens2(id,val){
    var target = document.getElementById(''+id+'');
    target.parentElement.innerHTML='启用';
        $.ajax({
            contentType: "application/json;charset=UTF-8",
            url:'https://'+ window.location.host +'/api/admin/amembership/crmAccount/updateStatus',
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            data: JSON.stringify({
                "id":id,
                "status":10,

            }),
            success: function (data) {

                target.onclick = null;
                target.setAttribute("onclick", 'confirm('+id+')')
                operatesearch();
            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        })

    layer.close(index);
}
//重置密码
var index=0;
function resetpassword(id,val) {
    var curInd = layer.confirm('<span id="status" style="width:200px;height:100px;resize: none;" maxlength="200" >确定要重置密码吗？</span>', {title:'提示',btn: ['确定', '取消']}, function(index){

        resetpassword1(id,curInd,val)
        layer.close(curInd);
    },function(){
        layer.close(curInd);

    });
}
//第一个
function resetpassword1(id,val,name){
    var target = document.getElementById(''+id+'');
        $.ajax({
            contentType: "application/json;charset=UTF-8",
            url:'https://'+ window.location.host +'/api/admin/amembership/crmAccount/resetPassword',
            cache: false,  //禁用缓存
            type: "POST",
            dataType:'json',
            data: JSON.stringify({
                "id":id,
                "loginAccount":name,

            }),
            success: function (data) {
                console.log(data)
                target.onclick = null;
                target.setAttribute("onclick", 'confirm('+id+')')
                if(data.errorCode==0){
                    $.Huimodalalert('重置密码成功！',2000);
                    operatesearch();
                }else {
                    $.Huimodalalert(data.msg,2000);
                }


            },
            error: function () {
                $.Huimodalalert('失败！',2000);
            }
        })
    layer.close(index);
}
